public class question_12 {
}

class Solution_12 {
    public String intToRoman(int num) {
        //先定义字符集合
        String romanStrings[]={"","I","V","X","L","C","D","M"};
        //先算出这个num是几位数
        int temp=num;
        int count=1;
        while (temp/10!=0){
            count++;
            temp=temp/10;
        }
        //count表示num是几位数
        String numToString[]=new String[count];
        if(count==4){
            //如果是四位数的话那么需要特殊的处理一下，在这道题中的数字不会超过3999，所以4位数的千位置需要特别的处理
            //获取4位数的第一位
            int four= (int) (num/Math.pow(10,3));
            String s="";
            while (four!=0){
                s+="M";
                four--;
            }
            num=num%1000;
            count=count-1;
            numToString[count]=s;
        }
        while (count!=0){
            //取num中第count位的数，注意这里是从右到左数的
            double t=Math.pow(10,count-1);
            int n= (int) (num/t);
            String one=romanStrings[count*2-1];
            String five= romanStrings[count*2];
            String ten=romanStrings[count*2+1];
            String s="";
            switch (n){
                case 9: s=one+ten;break;
                case 4:s=one+five;break;
                default:{
                    temp=n;
                    while (temp!=0){
                        if(temp>=5){
                            temp-=5;
                            s=s+five;
                            continue;
                        }
                        if(temp>0){
                            temp-=1;
                            s=s+one;
                        }
                    }
                }
            }
            numToString[count-1]=s;
            num= (int) (num%(Math.pow(10,count-1)));
            count--;
        }
        String result="";
        for (int i = numToString.length-1; i >=0 ; i--) {
            result=result+numToString[i];
        }
        return result;
    }

    public static void main(String[] args) {
        Solution_12 s = new Solution_12();
        System.out.println(s.intToRoman(1998));

    }
}